package com.lancoo.answer.view.fragment.composition.alternative;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lancoo.answer.R;
import com.lancoo.answer.model.bean.Child;


/**
 * Created by pengmi on 2022/5/25 17:12
 * Description:
 */
public class AlternativeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Child child;
    private OnAlternativeChoiceCallBack callBack;

    public AlternativeAdapter(Child child) {
        this.child = child;
    }

    public void setOnAlternativeChoiceCallBack(OnAlternativeChoiceCallBack callBack){
        this.callBack=callBack;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.ev_list_alternative_composition_item_layout, parent, false);
        return new VH(inflate,callBack);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        VH vh= (VH) holder;
        vh.initData(position,child.getSelectedItemIndex());

    }

    @Override
    public int getItemCount() {
        return child.getItemList().size();
    }

    private static class VH extends RecyclerView.ViewHolder implements View.OnClickListener{

        private OnAlternativeChoiceCallBack callBack;

        public VH(@NonNull View itemView, OnAlternativeChoiceCallBack callBack) {
            super(itemView);
            this.callBack=callBack;
            itemView.setOnClickListener(this);
        }

        private void initData(int position,int choiceIndex){
            TextView tv_order = itemView.findViewById(R.id.tv_order);
            tv_order.setText(String.format("题目 %s", position + 1));
            if(position==choiceIndex){
                tv_order.setTextColor(0xffffffff);
                tv_order.setBackgroundResource(R.drawable.ev_shape_corner_5_solid_09f);
            }else{
                tv_order.setTextColor(0xff999999);
                tv_order.setBackgroundResource(R.drawable.ev_shape_corner_5_solid_ed);
            }

        }

        @Override
        public void onClick(View v) {
            if(callBack!=null){
                callBack.onChoiceItem(getBindingAdapterPosition());
            }
        }
    }


    public abstract static class OnAlternativeChoiceCallBack{

        public void onChoiceItem(int position){}

    }
}
